// 请求通用方法
import apiMixin from '@/mixin/apiMixin';
import taskMixin from '@/mixin/taskMixin';
import uilts from '@/utils/index';

export default {
    mixins: [apiMixin,taskMixin],
    data() {
        return {
            tableData: [],
            handleShow:false,
            listNums: 0,
            // edit 修改 , 新增add
            type: 'edit',
            curIndex: -1,
            curUser: {
                account: '',
                pasd: '',
                qq: '',
                phone: ''
            },
            loading: false,
            afirmBtnLoad: false,
            curPage: 1,
        }
    },
    mounted() {
        this.init();
    },
    methods: {
        init() {
            this.getAdminAccountApi(this.curPage);
        },
        getAdminAccountApi(page) {
            let _self = this;
            this.comResquest(this.request.getAdminAccount,(res)=>{
                console.log(res);
                let { total_records,records } = res;
                _self.listNums = total_records;
                _self.tableData = records;
                _self.updateRoutePar(page);
            },{
                load: true,
                offset: page || 1
            });
        },
        handleEdit(editIndex) {
            this.type = 'edit';
            this.curIndex = editIndex;
            let curAccount = this.tableData[editIndex];
            this.curUser = {
                account: curAccount.login_name,
                pasd: '',
                qq: curAccount.qq,
                phone: curAccount.mobile
            }
            this.handleShow = true;
        },
        handleDelete(deleteIndex) {
            let curAccount = this.tableData[deleteIndex];
            let _self = this;
            this.$confirm('此操作将删除该管理员, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).then(() => {
                console.log('成功');
                _self.comResquest(_self.request.deleteAdminAccount,(res)=>{
                    _self.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    _self.init();
                },{
                    admin_id: curAccount.id,
                    load: true,
                });
            }).catch(() => {   

            });
        },
        initPopPar() {
            this.curUser =  {
                account: '',
                pasd: '',
                qq: '',
                phone: ''
            };
        },
        editAdminAccountApi() {
            let curUser = this.curUser;
            let resPar = {
                login_name: curUser.account,
                passwd: curUser.pasd,
                qq: curUser.qq,
                mobile: curUser.phone,
                load: "afirmBtnLoad",
            };
            if (this.type === 'edit') {
                let curAccount = this.tableData[this.curIndex];
                resPar.admin_id = curAccount.id;
            }
            let _self = this;
            this.comResquest(this.request.editAdminAccount,(res)=>{
                _self.$message.success('操作成功！');
                _self.handleShow = false;
                _self.init();
            },resPar);
        },
        cancelPop() {
            this.initPopPar();
            this.handleShow = false;
        },
        handleAdd() {
            this.initPopPar();
            this.type = 'add';
            this.handleShow = true;
        },
        handleAccount() {
            this.editAdminAccountApi();
        },
        countTrialDate(milliseconds) {
            let date = new Date(milliseconds);
            return uilts.Dateformat(date,"yyyy-MM-dd hh:mm:ss")
        },
        changePage(page){
            page = page || 1;
            this.getAdminAccountApi(page);
        },
    },
}